package com.yellowpage.ndc.action;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.struts2.ServletActionContext;

import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.Session;

import com.yellowpage.ndc.data.ProdGroup;
import com.yellowpage.ndc.data.ProdGroupHis;
import com.yellowpage.ndc.service.ProdClassService;
import com.yellowpage.ndc.service.ProdCustomerService;
import com.yellowpage.ndc.service.ProdGroupHisService;
import com.yellowpage.ndc.service.ProdGroupService;
import com.yellowpage.white.common.Configuration;
import com.yellowpage.white.struts.BaseAction;

public class ProdGroupAction extends BaseAction {

	public String RESULT_QUERY = "query";
	public String RESULT_ADD = "add";
	public String RESULT_EDIT = "edit";
	public String RESULT_PRODCLASSSEL = "prodClassSel";
	public String RESULT_MAMAGEREGIONSEL = "manageRegionSel";
	public String RESULT_MAMAGEREGIONSEL_TREE = "manageRegionSelTree";
	public String RESULT_PRODCLASSSELTWO = "prodClassSelTwo";
	public String RESULT_QUANTITY = "quantityUpdate";
	public String RESULT_GROUPORDER = "prodOrder";

	private Configuration configuration;

	public void setConfiguration(Configuration configuration) {
		this.configuration = configuration;
	}

	private List<ProdGroup> prodGroupList = new ArrayList<ProdGroup>();
	private List<Map> resultList = new ArrayList<Map>();
	private List<Map> List = new ArrayList<Map>();
	private ProdGroup prodGroup;
	private ProdGroupService prodGroupService;
	private ProdCustomerService prodCustomerService;
	private ProdClassService prodClassService;
	private ProdGroupHis prodGroupHis;
	private ProdGroupHisService prodGroupHisService;

	private String selectedId;
	private String[] selectedIds;
	private String message;

	private String query_prodClassCode1;
	private String query_prodClassName1;
	private String query_prodClassCode2;
	private String query_prodClassName2;
	private String query_groupId;
	private String query_name1;
	private String query_name2;
	private String query_subname1;
	private String query_subname2;
	private String query_subname3;
	private String query_subname4;
	private String query_tel;
	private String query_telExt;
	private String query_faxFlag;
	private String query_sb;
	private String query_listOrder;
	private String query_regionCode;
	private String query_regionName;
	private String query_cityCode;
	private String query_cityName;
	private String query_destrictCode;
	private String query_destrictName;
	private String query_areaCode;
	private String query_addr;
	private String query_zip;
	private String query_pyAddr;
	private String query_engAddr;
	private String query_poix;
	private String query_poiy;
	private String query_custStatus;
	private String query_custTypeCode;
	private String query_classMainId;
	private String query_orgId;
	private String query_url;
	private String query_weibo;
	private String query_weiboName;
	private String query_weixin;
	private String query_yixin;
	private String query_logo;
	private String query_safeStatus;
	private String query_bxName;
	private String query_pyName;
	private String query_engName;
	private String query_spName;
	private String query_prodClassId;
	private String query_modifyTime;
	private String query_modifyEmpId;
	private String query_inputTime;
	private String query_inputEmpId;
	private String query_id;
	private String query_inputEmpCode;
	private String query_modifyEmpCode;
	private String[] selectedUpdate;

	private static final int BUFFER_SIZE = 16 * 1024;
	private String uploadContentType;
	private String savePath;
	private File upload;
	private String uploadFileName;

	private String class_code;
	private String class_name;

	public void prepare() {
		super.prepare();

		if (this.selectedId != null && !this.selectedId.equals("")) {
			this.prodGroup = prodGroupService.getProdGroup(Long
					.parseLong(selectedId));
		}
	}

	public String queryByPage() {
		initPageRender();

		Map queryMap = new HashMap();
		queryMap.put("prodClassCode1", query_prodClassCode1);
		queryMap.put("prodClassName1", query_prodClassName1);
		queryMap.put("prodClassCode2", query_prodClassCode2);
		queryMap.put("prodClassName2", query_prodClassName2);
		queryMap.put("groupId", query_groupId);
		queryMap.put("name1", query_name1);
		queryMap.put("name2", query_name2);
		queryMap.put("subname1", query_subname1);
		queryMap.put("subname2", query_subname2);
		queryMap.put("subname3", query_subname3);
		queryMap.put("subname4", query_subname4);
		queryMap.put("tel", query_tel);
		queryMap.put("telExt", query_telExt);
		queryMap.put("faxFlag", query_faxFlag);
		queryMap.put("sb", query_sb);
		queryMap.put("listOrder", query_listOrder);
		queryMap.put("regionCode", query_regionCode);
		queryMap.put("regionName", query_regionName);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("destrictCode", query_destrictCode);
		queryMap.put("destrictName", query_destrictName);
		queryMap.put("areaCode", query_areaCode);
		queryMap.put("addr", query_addr);
		queryMap.put("zip", query_zip);
		queryMap.put("pyAddr", query_pyAddr);
		queryMap.put("engAddr", query_engAddr);
		queryMap.put("poix", query_poix);
		queryMap.put("poiy", query_poiy);
		queryMap.put("custStatus", query_custStatus);
		queryMap.put("custTypeCode", query_custTypeCode);
		queryMap.put("classMainId", query_classMainId);
		queryMap.put("orgId", query_orgId);
		queryMap.put("url", query_url);
		queryMap.put("weibo", query_weibo);
		queryMap.put("weiboName", query_weiboName);
		queryMap.put("weixin", query_weixin);
		queryMap.put("yixin", query_yixin);
		queryMap.put("logo", query_logo);
		queryMap.put("safeStatus", query_safeStatus);
		queryMap.put("bxName", query_bxName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("engName", query_engName);
		queryMap.put("spName", query_spName);
		queryMap.put("prodClassId", query_prodClassId);
		queryMap.put("modifyTime", query_modifyTime);
		queryMap.put("modifyEmpId", query_modifyEmpId);
		queryMap.put("inputTime", query_inputTime);
		queryMap.put("inputEmpId", query_inputEmpId);
		queryMap.put("id", query_id);
		queryMap.put("inputEmpCode", query_inputEmpCode);
		queryMap.put("modifyEmpCode", query_modifyEmpCode);

		resultList = prodGroupService
				.queryProdGroupByPage(queryMap, pageRender);

		setPageRenderToRequest();

		return RESULT_QUERY;
	}

	public void exportExcel() {
		Map queryMap = new HashMap();
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		resultList = prodGroupService.ProdGroupexcel(queryMap);
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("application/x-download");
		OutputStream outp = null;
		response.addHeader("Content-Disposition", "attachment;filename="
				+ "ProdGroup.xls");
		try {
			outp = response.getOutputStream();
			HSSFWorkbook workbook = new HSSFWorkbook();
			HSSFSheet sheet = workbook.createSheet("ProdGroup");
			HSSFRow row = sheet.createRow(0);
			sheet.autoSizeColumn((short) 0);
			row.createCell(0).setCellValue("一级分类编号 ");
			row.createCell(1).setCellValue("一级分类名称 ");
			row.createCell(2).setCellValue("二级分类编号 ");
			row.createCell(3).setCellValue("二级分类名称 ");
			row.createCell(4).setCellValue("品牌编号 ");
			row.createCell(5).setCellValue("品牌名称1 ");
			row.createCell(6).setCellValue("品牌名称2 ");
			row.createCell(7).setCellValue("区号 ");
			row.createCell(8).setCellValue("电话 ");
			row.createCell(9).setCellValue("分机 ");
			row.createCell(10).setCellValue("地区代码 ");
			row.createCell(11).setCellValue("地区名称 ");
			row.createCell(12).setCellValue("城市代码 ");
			row.createCell(13).setCellValue("城市名称 ");
			row.createCell(14).setCellValue("区代码 ");
			row.createCell(15).setCellValue("区名称 ");
			row.createCell(16).setCellValue("地址 ");
			row.createCell(17).setCellValue("邮编 ");
			row.createCell(18).setCellValue("Poix ");
			row.createCell(19).setCellValue("Poiy ");
			row.createCell(20).setCellValue("Url ");
			row.createCell(21).setCellValue("列名次序 ");
			row.createCell(22).setCellValue("微博 ");
			row.createCell(23).setCellValue("微信 ");
			row.createCell(24).setCellValue("易信 ");
			row.createCell(25).setCellValue("修改时间 ");
			row.createCell(26).setCellValue("修改人 ");
			row.createCell(27).setCellValue("输入时间 ");
			row.createCell(28).setCellValue("输入人 ");
			for (int i = 0; i < resultList.size(); i++) {

				HSSFRow data_row = sheet.createRow(i + 1);
				Map result = (Map) resultList.get(i);
				data_row.createCell(0).setCellValue(
						result.get("PROD_CLASS_CODE1") != null ? result.get(
								"PROD_CLASS_CODE1").toString() : "");
				data_row.createCell(1).setCellValue(
						result.get("PROD_CLASS_NAME1") != null ? result.get(
								"PROD_CLASS_NAME1").toString() : "");
				data_row.createCell(2).setCellValue(
						result.get("PROD_CLASS_CODE2") != null ? result.get(
								"PROD_CLASS_CODE2").toString() : "");
				data_row.createCell(3).setCellValue(
						result.get("PROD_CLASS_NAME2") != null ? result.get(
								"PROD_CLASS_NAME2").toString() : "");
				data_row.createCell(4).setCellValue(
						result.get("GROUP_ID") != null ? result.get("GROUP_ID")
								.toString() : "");
				data_row.createCell(5).setCellValue(
						result.get("NAME1") != null ? result.get("NAME1")
								.toString() : "");
				data_row.createCell(6).setCellValue(
						result.get("NAME2") != null ? result.get("NAME2")
								.toString() : "");
				data_row.createCell(7).setCellValue(
						result.get("AREA_CODE") != null ? result.get(
								"AREA_CODE").toString() : "");
				data_row.createCell(8).setCellValue(
						result.get("TEL") != null ? result.get("TEL")
								.toString() : "");
				data_row.createCell(9).setCellValue(
						result.get("TEL_EXT") != null ? result.get("TEL_EXT")
								.toString() : "");
				data_row.createCell(10).setCellValue(
						result.get("REGION_CODE") != null ? result.get(
								"REGION_CODE").toString() : "");
				data_row.createCell(11).setCellValue(
						result.get("REGION_NAME") != null ? result.get(
								"REGION_NAME").toString() : "");
				data_row.createCell(12).setCellValue(
						result.get("CITY_CODE") != null ? result.get(
								"CITY_CODE").toString() : "");
				data_row.createCell(13).setCellValue(
						result.get("CITY_NAME") != null ? result.get(
								"CITY_NAME").toString() : "");
				data_row.createCell(14).setCellValue(
						result.get("DESTRICT_CODE") != null ? result.get(
								"DESTRICT_CODE").toString() : "");
				data_row.createCell(15).setCellValue(
						result.get("DESTRICT_NAME") != null ? result.get(
								"DESTRICT_NAME").toString() : "");
				data_row.createCell(16).setCellValue(
						result.get("ADDR") != null ? result.get("ADDR")
								.toString() : "");
				data_row.createCell(17).setCellValue(
						result.get("ZIP") != null ? result.get("ZIP")
								.toString() : "");
				data_row.createCell(18).setCellValue(
						result.get("POIX") != null ? result.get("POIX")
								.toString() : "");
				data_row.createCell(19).setCellValue(
						result.get("POIY") != null ? result.get("POIY")
								.toString() : "");
				data_row.createCell(20).setCellValue(
						result.get("URL") != null ? result.get("URL")
								.toString() : "");
				data_row.createCell(21).setCellValue(
						result.get("LIST_ORDER") != null ? result.get(
								"LIST_ORDER").toString() : "");
				data_row.createCell(22).setCellValue(
						result.get("WEIBO") != null ? result.get("WEIBO")
								.toString() : "");
				data_row.createCell(23).setCellValue(
						result.get("WEIXIN") != null ? result.get("WEIXIN")
								.toString() : "");
				data_row.createCell(24).setCellValue(
						result.get("YIXIN") != null ? result.get("YIXIN")
								.toString() : "");
				data_row.createCell(25).setCellValue(
						result.get("MODIFY_TIME") != null ? result.get(
								"MODIFY_TIME").toString() : "");
				data_row.createCell(26).setCellValue(
						result.get("MODIFY_EMP_ID") != null ? result.get(
								"MODIFY_EMP_ID").toString() : "");
				data_row.createCell(27).setCellValue(
						result.get("INPUT_TIME") != null ? result.get(
								"INPUT_TIME").toString() : "");
				data_row.createCell(28).setCellValue(
						result.get("INPUT_EMP_ID") != null ? result.get(
								"INPUT_EMP_ID").toString() : "");
			}
			workbook.write(outp);
			response.flushBuffer();
			outp.flush();
			response.reset();

		} catch (Exception e) {
			log.error(e.getMessage(), e);
			this.addActionError("** 生成文件错误 !");
		} finally {
			if (outp != null) {
				try {
					outp.close();
				} catch (IOException e) {
					log.error(e.getMessage(), e);
				}
			}
		}
	}

	public void count() throws Exception {

		Map queryMap = new HashMap();
		queryMap.put("prodClassCode1", query_prodClassCode1);
		queryMap.put("prodClassName1", query_prodClassName1);
		queryMap.put("prodClassCode2", query_prodClassCode2);
		queryMap.put("prodClassName2", query_prodClassName2);
		queryMap.put("groupId", query_groupId);
		queryMap.put("name1", query_name1);
		queryMap.put("name2", query_name2);
		queryMap.put("subname1", query_subname1);
		queryMap.put("subname2", query_subname2);
		queryMap.put("subname3", query_subname3);
		queryMap.put("subname4", query_subname4);
		queryMap.put("tel", query_tel);
		queryMap.put("telExt", query_telExt);
		queryMap.put("faxFlag", query_faxFlag);
		queryMap.put("sb", query_sb);
		queryMap.put("listOrder", query_listOrder);
		queryMap.put("regionCode", query_regionCode);
		queryMap.put("regionName", query_regionName);
		queryMap.put("cityCode", query_cityCode);
		queryMap.put("cityName", query_cityName);
		queryMap.put("destrictCode", query_destrictCode);
		queryMap.put("destrictName", query_destrictName);
		queryMap.put("areaCode", query_areaCode);
		queryMap.put("addr", query_addr);
		queryMap.put("zip", query_zip);
		queryMap.put("pyAddr", query_pyAddr);
		queryMap.put("engAddr", query_engAddr);
		queryMap.put("poix", query_poix);
		queryMap.put("poiy", query_poiy);
		queryMap.put("custStatus", query_custStatus);
		queryMap.put("custTypeCode", query_custTypeCode);
		queryMap.put("classMainId", query_classMainId);
		queryMap.put("orgId", query_orgId);
		queryMap.put("url", query_url);
		queryMap.put("weibo", query_weibo);
		queryMap.put("weiboName", query_weiboName);
		queryMap.put("weixin", query_weixin);
		queryMap.put("yixin", query_yixin);
		queryMap.put("logo", query_logo);
		queryMap.put("safeStatus", query_safeStatus);
		queryMap.put("bxName", query_bxName);
		queryMap.put("pyName", query_pyName);
		queryMap.put("engName", query_engName);
		queryMap.put("spName", query_spName);
		queryMap.put("prodClassId", query_prodClassId);
		queryMap.put("modifyTime", query_modifyTime);
		queryMap.put("modifyEmpId", query_modifyEmpId);
		queryMap.put("inputTime", query_inputTime);
		queryMap.put("inputEmpId", query_inputEmpId);
		queryMap.put("id", query_id);
		queryMap.put("inputEmpCode", query_inputEmpCode);
		queryMap.put("modifyEmpCode", query_modifyEmpCode);

		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		resultList = prodGroupService.countProdGroup(queryMap);
		try {
			if (resultList != null) {
				out.print("总共" + resultList.get(0).get("TOTAL") + "条");
			} else {
				out.print("总共0条");
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			out.print("");
		}
		out.flush();
		out.close();

	}

	public String toAdd() {
		return RESULT_ADD;
	}

	public String toOrder() {
		return RESULT_GROUPORDER;
	}

	public String toProdClassSel() {
		return RESULT_PRODCLASSSEL;
	}

	public String toManageRegionSel() {
		return RESULT_MAMAGEREGIONSEL;
	}

	public String toManageRegionSelTree() {
		return RESULT_MAMAGEREGIONSEL_TREE;
	}

	public String toProdClassSelTwo() {
		return RESULT_PRODCLASSSELTWO;
	}

	public String save() throws IOException {
		String groupId = prodGroupService.getGroupId();
		groupId = String.valueOf((Integer.valueOf(groupId)).intValue() + 1);

		int j = 8 - groupId.length();
		for (int i = 0; i < j; i++) {
			groupId = "0" + groupId;
		}
		prodGroup.setGroupId("GFOF" + prodGroup.getRegionCode().substring(0, 4)
				+ groupId);

		String regionName = prodGroupService.getRegionByCode(prodGroup
				.getRegionCode());
		prodGroup.setRegionName(regionName);
		prodGroup.setRegionCode(prodGroup.getRegionCode());
		prodGroup.setCustStatus(new Double(0));

		// prodGroup.setListOrder(1);
		prodGroup.setInputEmpId(Integer.valueOf(loginUser.getUserId()
				.toString()));
		prodGroup.setInputTime(new Date());

		// 根据服务器的文件保存地址和原文件名创建目录文件全路径
		log.error("path"
				+ ServletActionContext.getServletContext().getRealPath(
						this.getSavePath()) + "\\" + this.getUploadFileName());
		log.error("uploadFileName is " + this.getUploadFileName());
		log.error("uploadContentType is " + this.getUploadContentType());

		String dstPath = ServletActionContext.getServletContext().getRealPath(
				this.getSavePath())
				+ "\\" + this.getUploadFileName();
		File dstFile = new File(dstPath);

		if (this.getUploadFileName() != null) {
			log.error("填了logo");
			// if(!this.getUploadContentType().equals("image/png")){
			// message = "新增失败!只能上传附件后缀名为png的图片!";
			// return RESULT_ADD;
			// } else {
			copy(this.upload, dstFile);

			Connection conn = new Connection("101.227.246.1");
			conn.connect();

			boolean isAuthenticated = conn.authenticateWithPassword("xujing",
					"hB#118114$123@");
			if (isAuthenticated == false) {
				throw new IOException("Authentication failed.");
			}

			SCPClient scpClient = conn.createSCPClient();
			// scpClient.put(dstPath,"/home/web/icon/test/");
			// scpClient.put(dstPath, (regionCode.substring(0, 4) +
			// groupId.substring((groupId.length() - 4), groupId.length())) +
			// ".png", "/home/web/icon/test/", "0644");
			scpClient.put(dstPath, ("GFOF"
					+ prodGroup.getRegionCode().substring(0, 4) + groupId)
					+ ".png", "/home/web/icon/omc/", "0644");

			// prodGroup.setLogo("/home/web/icon/" + (regionCode.substring(0, 4)
			// + groupId.substring((groupId.length() - 4), groupId.length())) +
			// ".png");
			prodGroup
					.setLogo("/icon/omc/"
							+ ("GFOF"
									+ prodGroup.getRegionCode().substring(0, 4) + groupId)
							+ ".png");
			// }
		}

		prodGroupService.saveProdGroup(prodGroup);
		ProdGroupHis prodGroupHis = new ProdGroupHis();
		String groupHis = "GFOF" + prodGroup.getRegionCode().substring(0, 4)
				+ groupId;
		List<Map> list = new ArrayList<Map>();
		list = prodGroupService.getIdByGroupId(groupHis);
		String prodgroupid = list.get(0).get("ID").toString();
		prodGroupHis.setContent("id = " + prodgroupid);
		prodGroupHis.setGroupId(Integer.parseInt(prodgroupid));

		prodGroupHis.setType(0);
		prodGroupHis.setCreateUser(Integer.valueOf(loginUser.getUserId()
				.toString()));
		prodGroupHis.setCreateTime(new Date());
		prodGroupHisService.saveProdGroupHis(prodGroupHis);

		message = "新增成功!";
		return RESULT_ADD;
	}

	private static void copy(File src, File dst) {
		InputStream in = null;
		OutputStream out = null;
		try {
			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
			out = new BufferedOutputStream(new FileOutputStream(dst),
					BUFFER_SIZE);
			byte[] buffer = new byte[BUFFER_SIZE];
			int len = 0;
			while ((len = in.read(buffer)) > 0) {
				out.write(buffer, 0, len);
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (null != in) {
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if (null != out) {
				try {
					out.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	public void validateSave() {
		if (prodGroup.getProdClassCode1().length() == 0) {
			this.addActionError("** 一级分类编号 不能为空 !");
		}
		if (prodGroup.getProdClassCode2().length() == 0) {
			this.addActionError("** 二级分类编号 不能为空 !");
		}
		if (prodGroup.getName1().length() == 0) {
			this.addActionError("** 品牌名称1 不能为空 !");
		}
		if (prodGroup.getTel().length() == 0) {
			this.addActionError("** 电话 不能为空 !");
		}
		if (prodGroup.getRegionCode().length() == 0) {
			this.addActionError("** 地区代码 不能为空 !");
		}
		if (prodGroup.getCityCode().length() == 0) {
			this.addActionError("** 城市代码 不能为空 !");
		}
		if (prodGroup.getDestrictCode().length() == 0) {
			this.addActionError("** 区代码 不能为空 !");
		}
		if (prodGroup.getListOrder() == null) {
			this.addActionError("** 列名次序 不能为空 !");
		}

		this.setValidatorResult("/ndc/ProdGroupAdd.jsp");
	}

	public String toEdit() {
		Long id = new Long(selectedId);

		prodGroup = prodGroupService.getProdGroup(id);

		return RESULT_EDIT;
	}

	public String update() throws IOException {
		String groupId = prodGroupService.getGroupId();

		String regionName = prodGroupService.getRegionByCode(prodGroup
				.getRegionCode());
		prodGroup.setRegionName(regionName);

		prodGroup.setModifyTime(new Date());
		prodGroup.setModifyEmpId(Integer.valueOf(loginUser.getUserId()
				.toString()));

		if (this.getUploadFileName() != null) {

			// prodGroup.setLogo("/icon/omc/" + ("GFOF" +
			// prodGroup.getRegionCode().substring(0, 4) + groupId) + ".png");
			// 根据服务器的文件保存地址和原文件名创建目录文件全路径
			log.error("path"
					+ ServletActionContext.getServletContext().getRealPath(
							this.getSavePath()) + "\\"
					+ this.getUploadFileName());
			log.error("uploadFileName is " + this.getUploadFileName());
			log.error("uploadContentType is " + this.getUploadContentType());

			String dstPath = ServletActionContext.getServletContext()
					.getRealPath(this.getSavePath())
					+ "\\"
					+ this.getUploadFileName();
			File dstFile = new File(dstPath);
			log.error("填了logo");
			// if(!this.getUploadContentType().equals("image/png")){
			// message = "新增失败!只能上传附件后缀名为png的图片!";
			// return RESULT_ADD;
			// } else {
			copy(this.upload, dstFile);

			Connection conn = new Connection("101.227.246.1");
			conn.connect();

			boolean isAuthenticated = conn.authenticateWithPassword("xujing",
					"hB#118114$123@");
			if (isAuthenticated == false) {
				throw new IOException("Authentication failed.");
			}

			SCPClient scpClient = conn.createSCPClient();
			// scpClient.put(dstPath,"/home/web/icon/test/");
			// scpClient.put(dstPath, (regionCode.substring(0, 4) +
			// groupId.substring((groupId.length() - 4), groupId.length())) +
			// ".png", "/home/web/icon/test/", "0644");
			scpClient.put(dstPath, prodGroup.getGroupId() + ".png",
					"/home/web/icon/omc/", "0644");

			// prodGroup.setLogo("/home/web/icon/" + (regionCode.substring(0, 4)
			// + groupId.substring((groupId.length() - 4), groupId.length())) +
			// ".png");
			prodGroup.setLogo("/icon/omc/" + prodGroup.getGroupId() + ".png");

			//Session session = conn.openSession();
			//session.execCommand("chmod 644 /home/web/icon/omc/" + prodGroup.getGroupId() + ".png");
			
			//session.close();
			conn.close();

			// }
		}

		ProdGroup prodGroupData = prodGroupService.getProdGroup(prodGroup
				.getId());
		JSONObject obj1 = JSONObject.fromObject(prodGroupData);
		JSONObject obj2 = JSONObject.fromObject(prodGroup);

		String content = "";

		for (int i = 0; i < obj2.names().size(); i++) {
			if (obj1.names().get(i).equals("modifyTime")
					|| obj1.names().get(i).equals("inputTime")) {
				continue;
			}
			if (!obj1.get(obj1.names().get(i)).equals(
					obj2.get(obj1.names().get(i)))) {

				content += obj1.get(obj1.names().get(i)) + "-->"
						+ obj2.get(obj1.names().get(i)) + ",";

			}
		}

		ProdGroupHis prodGroupHis = new ProdGroupHis();
		prodGroupHis.setGroupId(Integer.valueOf(prodGroup.getId().toString()));
		prodGroupHis.setContent(content);
		prodGroupHis.setType(1);
		prodGroupHis.setCreateUser(Integer.valueOf(loginUser.getUserId()
				.toString()));
		prodGroupHis.setCreateTime(new Date());
		prodGroupHisService.saveProdGroupHis(prodGroupHis);

		prodGroupService.updateProdGroup(prodGroup);
		message = "修改成功!";

		return RESULT_EDIT;
	}

	public String toQuantity() {
		Long id = new Long(selectedId);

		prodGroup = prodGroupService.getProdGroup(id);
		return RESULT_QUANTITY;
	}

	/*
	 * 批量修改（根据品牌编号进行修改）
	 */
	public String quantityUpdate() {
		Map<Integer, String> selectedMap = new HashMap<Integer, String>();
		if (selectedUpdate != null) {
			for (int i = 0; i < selectedUpdate.length; i++) {
				selectedMap.put(Integer.parseInt(selectedUpdate[i]), "true");
			}
			String content = "";

			List<Map> list = prodGroupService.getIdByGroupId(prodGroup
					.getGroupId());
			if (list.size() > 0) {
				for (Map map : list) {
					ProdGroup group = prodGroupService.getProdGroup(Long
							.parseLong(map.get("ID").toString()));

					if (selectedMap.get(1) != null) {
						content += group.getName1() + "-->"
								+ prodGroup.getName1();
						group.setName1(prodGroup.getName1());
					}
					if (selectedMap.get(2) != null) {
						content += group.getName2() + "-->"
								+ prodGroup.getName2() + " , ";
						group.setName2(prodGroup.getName2());
					}
					if (selectedMap.get(3) != null) {
						content += group.getSubname1() + "-->"
								+ prodGroup.getSubname1() + " , ";
						group.setSubname1(prodGroup.getSubname1());
					}
					if (selectedMap.get(4) != null) {
						content += group.getTel() + "-->" + prodGroup.getTel()
								+ " , ";
						group.setTel(prodGroup.getTel());
					}
					if (selectedMap.get(5) != null) {
						content += group.getRegionCode() + "-->"
								+ prodGroup.getRegionCode() + " , ";
						group.setRegionCode(prodGroup.getRegionCode());
					}
					if (selectedMap.get(6) != null) {
						content += group.getAddr() + "-->"
								+ prodGroup.getAddr() + " , ";
						group.setAddr(prodGroup.getAddr());
					}
					if (selectedMap.get(7) != null) {
						content += group.getZip() + "-->" + prodGroup.getZip()
								+ " , ";
						group.setZip(prodGroup.getZip());
					}
					if (selectedMap.get(8) != null) {
						content += group.getUrl() + "-->" + prodGroup.getUrl()
								+ " , ";
						group.setUrl(prodGroup.getUrl());
					}
					if (selectedMap.get(9) != null) {
						content += group.getWeibo() + "-->"
								+ prodGroup.getWeibo() + " , ";
						group.setWeibo(prodGroup.getWeibo());
					}
					if (selectedMap.get(10) != null) {
						content += group.getWeiboName() + "-->"
								+ prodGroup.getWeiboName() + " , ";
						group.setWeiboName(prodGroup.getWeiboName());
					}
					if (selectedMap.get(11) != null) {
						content += group.getWeixin() + "-->"
								+ prodGroup.getWeixin() + " , ";
						group.setWeixin(prodGroup.getWeixin());
					}
					if (selectedMap.get(12) != null) {
						content += group.getYixin() + "-->"
								+ prodGroup.getYixin() + " , ";
						group.setYixin(prodGroup.getYixin());
					}
					ProdGroupHis prodGroupHis = new ProdGroupHis();
					prodGroupHis.setGroupId(Integer.valueOf(prodGroup.getId()
							.toString()));
					prodGroupHis.setContent(content);
					prodGroupHis.setType(1);
					prodGroupHis.setCreateUser(Integer.valueOf(loginUser
							.getUserId().toString()));
					prodGroupHis.setCreateTime(new Date());
					prodGroupHisService.saveProdGroupHis(prodGroupHis);

					group.setModifyTime(new Date());
					group.setModifyEmpId(Integer.valueOf(loginUser.getUserId()
							.toString()));
					prodGroupService.updateProdGroup(group);

				}
			}
			message = "修改成功!";
		} else {
			message = "您没有选择需要修改的字段";
		}

		return RESULT_QUANTITY;
	}

	public void validateUpdate() {
		if (prodGroup.getProdClassCode1().length() == 0) {
			this.addActionError("** 一级分类编号 不能为空 !");
		}
		if (prodGroup.getProdClassCode2().length() == 0) {
			this.addActionError("** 二级分类编号 不能为空 !");
		}
		if (prodGroup.getName1().length() == 0) {
			this.addActionError("** 品牌名称1 不能为空 !");
		}
		if (prodGroup.getTel().length() == 0) {
			this.addActionError("** 电话 不能为空 !");
		}
		if (prodGroup.getRegionCode().length() < 4) {
			this.addActionError("** 地区代码 不能小于4位 !");
		}
		if (prodGroup.getCityCode().length() == 0) {
			this.addActionError("** 城市代码 不能为空 !");
		}
		if (prodGroup.getDestrictCode().length() == 0) {
			this.addActionError("** 区代码 不能为空 !");
		}
		if (prodGroup.getListOrder() == null) {
			this.addActionError("** 列名次序 不能为空 !");
		}

		this.setValidatorResult("/ndc/ProdGroupUpdate.jsp");
	}

	// 根据分类code获取分类名称
	public void getProdClassByCode() throws Exception {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/xml; charset=utf-8");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		class_name = prodClassService.getCodeName(class_code);
		try {
			if (class_name != null) {
				out.print(class_name);
			} else {
				out.print("");
			}

		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			out.print("");
		}
		out.flush();
		out.close();

	}

	public String delete() {

		if (!selectedIds.equals("")) {
			for (String id : selectedIds) {
				ProdGroup prodGroup = new ProdGroup();
				prodGroup = prodGroupService.getProdGroup(Long.parseLong(id));
				prodCustomerService.emptyGroupid(prodGroup.getGroupId());

				/*
				 * List<Map>
				 * customerList=prodCustomerService.getProdCustomerByGroupId
				 * (prodGroup.getGroupId()); if(customerList.size()>0){ String
				 * customerId=customerList.get(0).get("ID").toString();
				 * ProdCustomer prodCustomer=
				 * prodCustomerService.getProdCustomer
				 * (Long.parseLong(customerId));
				 * 
				 * 
				 * prodCustomer.setGroupId("");
				 * prodCustomerService.updateProdCustomer(prodCustomer); }
				 */

				ProdGroupHis prodGroupHis = new ProdGroupHis();
				prodGroupHis.setGroupId(Integer.valueOf(prodGroup.getId()
						.toString()));
				prodGroupHis.setContent("id : "
						+ Integer.valueOf(prodGroup.getId().toString())
						+ ",  groupId : " + prodGroup.getGroupId()+", name1 : "+prodGroup.getName1());
				prodGroupHis.setType(2);
				prodGroupHis.setCreateUser(Integer.valueOf(loginUser
						.getUserId().toString()));
				prodGroupHis.setCreateTime(new Date());
				prodGroupHisService.saveProdGroupHis(prodGroupHis);

				List ids = new ArrayList();
				ids.add(Long.valueOf(id));

				prodGroupService.delete(ids);
				message = "删除成功!";
			}
		}
		return queryByPage();
	}

	/**
	 * getters and setters
	 * 
	 * @return
	 */
	public void setProdGroup(ProdGroup prodGroup) {
		this.prodGroup = prodGroup;
	}

	public ProdGroup getProdGroup() {
		return prodGroup;
	}

	public void setProdGroupService(ProdGroupService prodGroupService) {
		this.prodGroupService = prodGroupService;
	}

	public List<ProdGroup> getProdGroupList() {
		return prodGroupList;
	}

	public void setProdGroupList(List<ProdGroup> prodGroupList) {
		this.prodGroupList = prodGroupList;
	}

	public String getSelectedId() {
		return selectedId;
	}

	public void setSelectedId(String selectedId) {
		this.selectedId = selectedId;
	}

	public String[] getSelectedIds() {
		return selectedIds;
	}

	public void setSelectedIds(String[] selectedIds) {
		this.selectedIds = selectedIds;
	}

	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	public List<Map> getResultList() {
		return resultList;
	}

	public void setResultList(List<Map> resultList) {
		this.resultList = resultList;
	}

	public String getQuery_prodClassCode1() {
		return this.query_prodClassCode1;
	}

	public void setQuery_prodClassCode1(String prodClassCode1) {
		this.query_prodClassCode1 = prodClassCode1;
	}

	public String getQuery_prodClassName1() {
		return this.query_prodClassName1;
	}

	public void setQuery_prodClassName1(String prodClassName1) {
		this.query_prodClassName1 = prodClassName1;
	}

	public String getQuery_prodClassCode2() {
		return this.query_prodClassCode2;
	}

	public void setQuery_prodClassCode2(String prodClassCode2) {
		this.query_prodClassCode2 = prodClassCode2;
	}

	public String getQuery_prodClassName2() {
		return this.query_prodClassName2;
	}

	public void setQuery_prodClassName2(String prodClassName2) {
		this.query_prodClassName2 = prodClassName2;
	}

	public String getQuery_groupId() {
		return this.query_groupId;
	}

	public void setQuery_groupId(String groupId) {
		this.query_groupId = groupId;
	}

	public String getQuery_name1() {
		return this.query_name1;
	}

	public void setQuery_name1(String name1) {
		this.query_name1 = name1;
	}

	public String getQuery_name2() {
		return this.query_name2;
	}

	public void setQuery_name2(String name2) {
		this.query_name2 = name2;
	}

	public String getQuery_subname1() {
		return this.query_subname1;
	}

	public void setQuery_subname1(String subname1) {
		this.query_subname1 = subname1;
	}

	public String getQuery_subname2() {
		return this.query_subname2;
	}

	public void setQuery_subname2(String subname2) {
		this.query_subname2 = subname2;
	}

	public String getQuery_subname3() {
		return this.query_subname3;
	}

	public void setQuery_subname3(String subname3) {
		this.query_subname3 = subname3;
	}

	public String getQuery_subname4() {
		return this.query_subname4;
	}

	public void setQuery_subname4(String subname4) {
		this.query_subname4 = subname4;
	}

	public String getQuery_tel() {
		return this.query_tel;
	}

	public void setQuery_tel(String tel) {
		this.query_tel = tel;
	}

	public String getQuery_telExt() {
		return this.query_telExt;
	}

	public void setQuery_telExt(String telExt) {
		this.query_telExt = telExt;
	}

	public String getQuery_faxFlag() {
		return this.query_faxFlag;
	}

	public void setQuery_faxFlag(String faxFlag) {
		this.query_faxFlag = faxFlag;
	}

	public String getQuery_sb() {
		return this.query_sb;
	}

	public void setQuery_sb(String sb) {
		this.query_sb = sb;
	}

	public String getQuery_listOrder() {
		return this.query_listOrder;
	}

	public void setQuery_listOrder(String listOrder) {
		this.query_listOrder = listOrder;
	}

	public String getQuery_regionCode() {
		return this.query_regionCode;
	}

	public void setQuery_regionCode(String regionCode) {
		this.query_regionCode = regionCode;
	}

	public String getQuery_regionName() {
		return this.query_regionName;
	}

	public void setQuery_regionName(String regionName) {
		this.query_regionName = regionName;
	}

	public String getQuery_cityCode() {
		return this.query_cityCode;
	}

	public void setQuery_cityCode(String cityCode) {
		this.query_cityCode = cityCode;
	}

	public String getQuery_cityName() {
		return this.query_cityName;
	}

	public void setQuery_cityName(String cityName) {
		this.query_cityName = cityName;
	}

	public String getQuery_destrictCode() {
		return this.query_destrictCode;
	}

	public void setQuery_destrictCode(String destrictCode) {
		this.query_destrictCode = destrictCode;
	}

	public String getQuery_destrictName() {
		return this.query_destrictName;
	}

	public void setQuery_destrictName(String destrictName) {
		this.query_destrictName = destrictName;
	}

	public String getQuery_areaCode() {
		return this.query_areaCode;
	}

	public void setQuery_areaCode(String areaCode) {
		this.query_areaCode = areaCode;
	}

	public String getQuery_addr() {
		return this.query_addr;
	}

	public void setQuery_addr(String addr) {
		this.query_addr = addr;
	}

	public String getQuery_zip() {
		return this.query_zip;
	}

	public void setQuery_zip(String zip) {
		this.query_zip = zip;
	}

	public String getQuery_pyAddr() {
		return this.query_pyAddr;
	}

	public void setQuery_pyAddr(String pyAddr) {
		this.query_pyAddr = pyAddr;
	}

	public String getQuery_engAddr() {
		return this.query_engAddr;
	}

	public void setQuery_engAddr(String engAddr) {
		this.query_engAddr = engAddr;
	}

	public String getQuery_poix() {
		return this.query_poix;
	}

	public void setQuery_poix(String poix) {
		this.query_poix = poix;
	}

	public String getQuery_poiy() {
		return this.query_poiy;
	}

	public void setQuery_poiy(String poiy) {
		this.query_poiy = poiy;
	}

	public String getQuery_custStatus() {
		return this.query_custStatus;
	}

	public void setQuery_custStatus(String custStatus) {
		this.query_custStatus = custStatus;
	}

	public String getQuery_custTypeCode() {
		return this.query_custTypeCode;
	}

	public void setQuery_custTypeCode(String custTypeCode) {
		this.query_custTypeCode = custTypeCode;
	}

	public String getQuery_classMainId() {
		return this.query_classMainId;
	}

	public void setQuery_classMainId(String classMainId) {
		this.query_classMainId = classMainId;
	}

	public String getQuery_orgId() {
		return this.query_orgId;
	}

	public void setQuery_orgId(String orgId) {
		this.query_orgId = orgId;
	}

	public String getQuery_url() {
		return this.query_url;
	}

	public void setQuery_url(String url) {
		this.query_url = url;
	}

	public String getQuery_weibo() {
		return this.query_weibo;
	}

	public void setQuery_weibo(String weibo) {
		this.query_weibo = weibo;
	}

	public String getQuery_weixin() {
		return this.query_weixin;
	}

	public void setQuery_weixin(String weixin) {
		this.query_weixin = weixin;
	}

	public String getQuery_yixin() {
		return this.query_yixin;
	}

	public void setQuery_yixin(String yixin) {
		this.query_yixin = yixin;
	}

	public String getQuery_logo() {
		return this.query_logo;
	}

	public void setQuery_logo(String logo) {
		this.query_logo = logo;
	}

	public String getQuery_safeStatus() {
		return this.query_safeStatus;
	}

	public void setQuery_safeStatus(String safeStatus) {
		this.query_safeStatus = safeStatus;
	}

	public String getQuery_bxName() {
		return this.query_bxName;
	}

	public void setQuery_bxName(String bxName) {
		this.query_bxName = bxName;
	}

	public String getQuery_pyName() {
		return this.query_pyName;
	}

	public void setQuery_pyName(String pyName) {
		this.query_pyName = pyName;
	}

	public String getQuery_engName() {
		return this.query_engName;
	}

	public void setQuery_engName(String engName) {
		this.query_engName = engName;
	}

	public String getQuery_spName() {
		return this.query_spName;
	}

	public void setQuery_spName(String spName) {
		this.query_spName = spName;
	}

	public String getQuery_prodClassId() {
		return this.query_prodClassId;
	}

	public void setQuery_prodClassId(String prodClassId) {
		this.query_prodClassId = prodClassId;
	}

	public String getQuery_modifyTime() {
		return this.query_modifyTime;
	}

	public void setQuery_modifyTime(String modifyTime) {
		this.query_modifyTime = modifyTime;
	}

	public String getQuery_modifyEmpId() {
		return this.query_modifyEmpId;
	}

	public void setQuery_modifyEmpId(String modifyEmpId) {
		this.query_modifyEmpId = modifyEmpId;
	}

	public String getQuery_inputTime() {
		return this.query_inputTime;
	}

	public void setQuery_inputTime(String inputTime) {
		this.query_inputTime = inputTime;
	}

	public String getQuery_inputEmpId() {
		return this.query_inputEmpId;
	}

	public void setQuery_inputEmpId(String inputEmpId) {
		this.query_inputEmpId = inputEmpId;
	}

	public String getQuery_id() {
		return this.query_id;
	}

	public void setQuery_id(String id) {
		this.query_id = id;
	}

	public String getQuery_inputEmpCode() {
		return query_inputEmpCode;
	}

	public void setQuery_inputEmpCode(String queryInputEmpCode) {
		query_inputEmpCode = queryInputEmpCode;
	}

	public String getQuery_modifyEmpCode() {
		return query_modifyEmpCode;
	}

	public void setQuery_modifyEmpCode(String queryModifyEmpCode) {
		query_modifyEmpCode = queryModifyEmpCode;
	}

	public String getQuery_weiboName() {
		return query_weiboName;
	}

	public void setQuery_weiboName(String queryWeiboName) {
		query_weiboName = queryWeiboName;
	}

	public String getSavePath() {
		return savePath;
	}

	public void setSavePath(String savePath) {
		this.savePath = savePath;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadContentType() {
		return uploadContentType;
	}

	public void setUploadContentType(String uploadContentType) {
		this.uploadContentType = uploadContentType;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public String[] getSelectedUpdate() {
		return selectedUpdate;
	}

	public void setSelectedUpdate(String[] selectedUpdate) {
		this.selectedUpdate = selectedUpdate;
	}

	public ProdCustomerService getProdCustomerService() {
		return prodCustomerService;
	}

	public void setProdCustomerService(ProdCustomerService prodCustomerService) {
		this.prodCustomerService = prodCustomerService;
	}

	public ProdGroupService getProdGroupService() {
		return prodGroupService;
	}

	public ProdClassService getProdClassService() {
		return prodClassService;
	}

	public void setProdClassService(ProdClassService prodClassService) {
		this.prodClassService = prodClassService;
	}

	public String getClass_code() {
		return class_code;
	}

	public void setClass_code(String class_code) {
		this.class_code = class_code;
	}

	public String getClass_name() {
		return class_name;
	}

	public void setClass_name(String class_name) {
		this.class_name = class_name;
	}

	public List<Map> getList() {
		return List;
	}

	public void setList(List<Map> list) {
		List = list;
	}

	public ProdGroupHis getProdGroupHis() {
		return prodGroupHis;
	}

	public void setProdGroupHis(ProdGroupHis prodGroupHis) {
		this.prodGroupHis = prodGroupHis;
	}

	public ProdGroupHisService getProdGroupHisService() {
		return prodGroupHisService;
	}

	public void setProdGroupHisService(ProdGroupHisService prodGroupHisService) {
		this.prodGroupHisService = prodGroupHisService;
	}
	
	public static void main(String[] args){
		
		try{
			Connection conn = new Connection("101.227.246.1");
		
		conn.connect();

		boolean isAuthenticated = conn.authenticateWithPassword("xujing",
				"hB#118114$123@");
		if (isAuthenticated == false) {
			throw new IOException("Authentication failed.");
		}

		SCPClient scpClient = conn.createSCPClient();
		// scpClient.put(dstPath,"/home/web/icon/test/");
		// scpClient.put(dstPath, (regionCode.substring(0, 4) +
		// groupId.substring((groupId.length() - 4), groupId.length())) +
		// ".png", "/home/web/icon/test/", "0644");
		scpClient.put("C:\\PROJECTS\\UBSS\\114黄页\\02_代码开发\\01_WORKSPACE\\OMC\\testOMC.png", 
				"testOMC.png",
				"/home/web/icon/omc/", "0644");

		// prodGroup.setLogo("/home/web/icon/" + (regionCode.substring(0, 4)
		// + groupId.substring((groupId.length() - 4), groupId.length())) +
		// ".png");
		
		//Session session = conn.openSession();
		//session.execCommand("chmod 644 /home/web/icon/omc/" + prodGroup.getGroupId() + ".png");
		
		//session.close();
		conn.close();
		
		}catch(Exception e){
			e.printStackTrace();
		}
		
	}

}
